package com.winit.schedule.job.tracker.event.persist;

import java.util.ArrayList;
import java.util.List;

import com.winit.schedule.core.event.AbstractEvent;
import com.winit.schedule.core.repository.constants.TaskItemStatus;
import com.winit.schedule.core.repository.entity.TaskItemEntity;
import com.winit.schedule.core.repository.entity.TaskRunRecordEntity;
import com.winit.schedule.core.repository.entity.TaskRunnerEntity;
import com.winit.schedule.core.repository.entity.TaskTrackerEntity;
import com.winit.schedule.core.utils.Clock;

public class DataPersistEvent extends AbstractEvent<DataPersistEventType> {

    private TaskTrackerEntity    taskTracker;

    private TaskRunRecordEntity  taskRunRecord;

    private List<TaskItemEntity> taskItemEntities;

    private List<String>         taskItemIdList;

    public DataPersistEvent(DataPersistEventType type, TaskTrackerEntity taskTracker, List<TaskRunnerEntity> taskRunners){
        super(type);
        this.taskTracker = taskTracker;
        if (null != taskRunners && taskRunners.size() > 0) {
            this.taskItemIdList = new ArrayList<String>(taskRunners.size());
            for (TaskRunnerEntity taskRunner : taskRunners) {
                taskItemIdList.addAll(taskRunner.getTaskItemIds());
            }
        }
    }

    public DataPersistEvent(DataPersistEventType type, TaskRunRecordEntity taskRunRecord){
        super(type);
        this.taskRunRecord = taskRunRecord;
    }

    public DataPersistEvent(DataPersistEventType type, TaskRunnerEntity taskRunner,
                            List<TaskItemEntity> taskItemEntities, TaskItemStatus taskItemStatus){
        super(type);
        if (null != taskItemEntities && !taskItemEntities.isEmpty()) {
            for (TaskItemEntity item : taskItemEntities) {
                item.setTaskRunner(taskRunner);
                item.setTaskItemStatus(taskItemStatus);
            }
            this.taskItemEntities = taskItemEntities;
        }
    }

    public DataPersistEvent(DataPersistEventType type, List<TaskRunnerEntity> taskRunners, TaskItemStatus taskItemStatus){
        super(type);

        if (null != taskRunners && taskRunners.size() > 0) {
            TaskItemEntity taskItem = null;
            this.taskItemEntities = new ArrayList<TaskItemEntity>(taskRunners.size());
            for (TaskRunnerEntity taskRunner : taskRunners) {
                taskItem = new TaskItemEntity();
                taskItem.setTaskRunner(taskRunner);
                taskItem.setTaskItemStatus(taskItemStatus);
                taskItem.setLastStatusUpdateTime(Clock.getSystemTime());
                taskItemEntities.add(taskItem);
            }
        }
    }

    public TaskTrackerEntity getTaskTracker() {
        return taskTracker;
    }

    public TaskRunRecordEntity getTaskRunRecord() {
        return taskRunRecord;
    }

    public List<TaskItemEntity> getTaskItemEntities() {
        return taskItemEntities;
    }

    public List<String> getTaskItemIdList() {
        return taskItemIdList;
    }

    public void setTaskItemIdList(List<String> taskItemIdList) {
        this.taskItemIdList = taskItemIdList;
    }

}
